//
//  MainTabBar.swift
//  ZYBWeibo
//
//  Created by xthink4 on 16/2/23.
//  Copyright © 2016年 zyb. All rights reserved.
//

import UIKit
class MainTabBar: UITabBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        //print("\(__FUNCTION__)")
       // print(self.subviews as NSArray)
        //均分tabBar的按钮
        let countBtn=5//按钮的个数
        let w=self.bounds.size.width / CGFloat(countBtn)//按钮的宽度
        let h=self.bounds.size.height//按钮的高度
        var index=0
        let frame=CGRectMake(0, 0, w, h)
        for v in subviews{
            if v is UIControl && !(v is UIButton){
                v.frame=CGRectMake(CGFloat(index) * w , 0, w, h)
//                index++
//                if index==2{//找到中间的位置
//                    index++
//                }
                //三目运算符
                index+=(index==1) ? 2 : 1
            }
        }
        self.customButton.frame=frame
        //设置按钮的位置
        self.customButton.center=CGPointMake(self.center.x, h * 0.5)
    }
    //懒加载（创建中间发布按钮）
    lazy var customButton:UIButton={
        let btn=UIButton()
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        self.addSubview(btn)//将按钮添加在tabBar上面
        return btn
    }()
}
